<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Notifications
 * @description : Notifications for web, addons, etc..
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Notification extends AUTH_Controller {

  /**
   * Notifications ajax
   * 
   */
  public function index() {

	// Checking request type
	if ($this->is_post() && $this->is_ajax()) {

	  // Init vars
	  $response = array();
	  $response['status'] = 1;
	  $response['message'] = 'success';
	  $response['data'] = NULL;

	  // Injecting datas
	  $response['data']['header_counts'] = $this->header_counts();
	  $response['data']['toast_notify'] = $this->notify_toast();
	  $response['data']['unread_notify_count'] = $this->notify_list_count();

	  // Responding json
	  die(json_encode($response));
	}

	// Redirect if invalid request
	redirect();
  }

  /**
   * For Addons
   * 
   */
  public function addon() {

	// Stoped Due to Server Load Issue by HostMonster
	die();

	// Notification count
	$hdr_counts = $this->header_counts();
	$total_count = (int) $hdr_counts['tweets'] + $hdr_counts['albums'] + $hdr_counts['polls'] + $hdr_counts['events'];

	$items = array();

	if (!empty($hdr_counts['tweets'])) {
	  $tweets = $this->tweet_model->get_tweets(1);
	  if (!empty($tweets['result'][0])) {
		$items[] = array('type' => 'tweet', 'username' => $tweets['result'][0]['user_name'], 'title' => $tweets['result'][0]['tweet_subject'], 'date' => $tweets['result'][0]['tweet_date'], 'url' => site_url('redirect?post_id=' . make_post_id('tweet', $tweets['result'][0]['tweet_id'])));
	  }
	}

	if (!empty($hdr_counts['albums'])) {
	  $albums = $this->album_model->get_albums(null, 1);
	  if (!empty($albums['result'][0])) {
		$items[] = array('type' => 'album', 'username' => $albums['result'][0]['user_name'], 'title' => $albums['result'][0]['album_name'], 'date' => $albums['result'][0]['album_date'], 'url' => site_url('redirect?post_id=' . make_post_id('album', $albums['result'][0]['album_id'])));
	  }
	}

	if (!empty($hdr_counts['polls'])) {
	  $polls = $this->poll_model->get_polls(1);
	  if (!empty($polls['result'][0])) {
		$items[] = array('type' => 'poll', 'username' => $polls['result'][0]['user_name'], 'title' => $polls['result'][0]['poll_message'], 'date' => $polls['result'][0]['poll_date'], 'url' => site_url('redirect?post_id=' . make_post_id('poll', $polls['result'][0]['poll_id'])));
	  }
	}

	// Birthdays
	$birthdays = $this->event_model->get_birthdays(date('Y-m-d'));
	if (!empty($birthdays['result'][0])) {
	  $items[] = array('type' => 'birthday', 'username' => $birthdays['result'][0]['user_name'], 'title' => 'Happy Birthday ' . $birthdays['result'][0]['user_display_name'], 'date' => $birthdays['result'][0]['user_birthday_date'], 'url' => site_url('wall/' . $birthdays['result'][0]['user_name']));
	}

	// Joindays
	$joindays = $this->event_model->get_joindays(date('Y-m-d'));
	if (!empty($joindays['result'][0])) {
	  $items[] = array('type' => 'joinday', 'username' => $joindays['result'][0]['user_name'], 'title' => 'Congrats ' . $joindays['result'][0]['user_display_name'], 'date' => $joindays['result'][0]['user_join_date'], 'url' => site_url('wall/' . $joindays['result'][0]['user_name']));
	}

	// User Event
	$user_event = $this->event_model->get_user_event(date('Y-m-d'));
	if (!empty($user_event['result'][0])) {
	  $items[] = array('type' => 'event', 'username' => $user_event['result'][0]['user_name'], 'title' => $user_event['result'][0]['event_message'], 'date' => $user_event['result'][0]['event_date'], 'url' => site_url('events'));
	}

	if (!empty($this->current_user)) {
	  $user_notifications = $this->cache->get('user_notify_' . $this->current_user['user_id']);
	  if (!empty($user_notifications) && is_array($user_notifications)) {
		$total_count = $total_count + count($user_notifications);
		foreach ($user_notifications as $notification) {
		  if (!empty($notification)) {
			$items[] = array('type' => 'notify', 'username' => $this->current_user['user_name'], 'title' => $notification['text'], 'date' => $notification['date'], 'url' => site_url('redirect?post_id=' . $notification['post_id']));
		  }
		}
	  }
	}

	@usort($items, array('notification', 'date_compare'));

	$response = array();
	$response['count'] = $total_count;
	if (!empty($items[0])) {

	  // Set Notify Title
	  if ($items[0]['type'] == 'notify') {
		$response['title'] = 'Notification for ' . '@' . $items[0]['username'];
	  } elseif ($items[0]['type'] == 'birthday') {
		$response['title'] = 'Birthday of ' . '@' . $items[0]['username'];
	  } elseif ($items[0]['type'] == 'joinday') {
		$response['title'] = 'Joinday of ' . '@' . $items[0]['username'];
	  } else {
		$response['title'] = ucwords($items[0]['type']) . ' by ' . '@' . $items[0]['username'];
	  }

	  // Other Notify Data
	  $response['text'] = mb_substr(strip_tags($items[0]['title']), 0, 50) . '...';
	  $response['url'] = $items[0]['url'];
	} else {
	  $response['title'] = '';
	  $response['text'] = '';
	  $response['url'] = '';
	}

	die(json_encode($response));
  }

  /**
   * Counts for the header menu
   * 
   */
  private function header_counts() {

	// Getting header counts
	$header_counts = $this->settings_model->get_header_counts();

	// Preparing response array
	$response = array();
	$response['tweets'] = (int) $header_counts['result']['tweet_total_count'];
	$response['events'] = (int) $header_counts['result']['event_total_count'] + $header_counts['result']['birthday_total_count'] + $header_counts['result']['joinday_total_count'];
	$response['albums'] = (int) $header_counts['result']['album_total_count'];
	$response['polls'] = (int) $header_counts['result']['poll_total_count'];
	$response['groups'] = (int) 0;
	$response['online'] = (int) $header_counts['result']['online_total_count'];

	// Responding json
	return $response;
  }

  /**
   * Preparing toast notificaion text
   * 
   */
  private function notify_toast() {

	$notify = array();

	if (!empty($this->current_user)) {

	  // Getting notifications from cache
	  $user_notifications = $this->cache->get('user_notify_' . $this->current_user['user_id']);

	  // Preparing notifications
	  if (!empty($user_notifications) && is_array($user_notifications)) {
		foreach ($user_notifications as $notification) {
		  if (!empty($notification)) {
			if (($notification['toast'] == 0)) {
			  $notify[] = (string) $notification['text'];
			}
		  }
		}
	  }

	  // Update toast viewed status
	  $this->cache->delete('user_notify_' . $this->current_user['user_id']);
	  $this->cache->save('user_notify_' . $this->current_user['user_id'], array_update($user_notifications, 'toast', 1), 32400);
	}

	return $notify;
  }

  /**
   * Preparing toast notificaion list data (Ajax)
   * 
   */
  public function notify_list() {

	// Checking request type
	if ($this->is_post() && $this->is_ajax()) {

	  $notify = NULL;

	  if (!empty($this->current_user)) {

		// Getting notifications from cache
		$user_notifications = $this->cache->get('user_notify_' . $this->current_user['user_id']);

		// Preparing notifications (Show recent 10 items)
		$count = 0;
		if (!empty($user_notifications) && is_array($user_notifications)) {
		  @usort($user_notifications, array('notification', 'date_compare'));
		  foreach ($user_notifications as $notification) {
			if (!empty($notification) && $count < 11) {
			  $notify = $notify . '<li><a href="' . site_url('redirect?post_id=' . @$notification['post_id']) . '" style="word-wrap: break-word; white-space: normal; cursor: pointer;">' . @$this->sanitize_html($notification['text']) . '</a></li>';
			}
			$count++;
		  }
		}

		// Update toast viewed status
		$this->cache->delete('user_notify_' . $this->current_user['user_id']);
		$this->cache->save('user_notify_' . $this->current_user['user_id'], array_update($user_notifications, 'read', 1), 32400);
	  }

	  if (empty($notify)) {
		$notify = '<li><a style="word-wrap: break-word; white-space: normal; cursor: pointer;"><span style="margin-left: 85px;">No Notifications</span></a></li>';
	  }

	  die($notify);
	}

	redirect();
  }

  /**
   * Preparing toast notificaion list unread count
   * 
   */
  public function notify_list_count() {

	$notify = array();

	if (!empty($this->current_user)) {

	  // Getting notifications from cache
	  $user_notifications = $this->cache->get('user_notify_' . $this->current_user['user_id']);

	  // Preparing notifications
	  if (!empty($user_notifications) && is_array($user_notifications)) {
		foreach ($user_notifications as $notification) {
		  if (!empty($notification)) {
			if (($notification['read'] == 0)) {
			  $notify[] = (string) $notification['text'];
			}
		  }
		}
	  }
	}

	return count($notify);
  }

  private function date_compare($a, $b) {
	$t1 = strtotime(date('Y-m-d H:i:s', strtotime($a['date'])));
	$t2 = strtotime(date('Y-m-d H:i:s', strtotime($b['date'])));
	return $t2 - $t1;
  }

}
